package at.ac.thornwishstalon.ocrapp.main;


import at.ac.thornwishstalon.ocrapp.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import at.ac.thornwishstalon.ocrapp.main.fragments.ImageFragment;
import at.ac.thornwishstalon.ocrapp.main.fragments.OCRAPPDialogHandler;
import at.ac.thornwishstalon.ocrapp.main.fragments.OcrappDialogFragment;
import at.ac.thornwishstalon.ocrapp.main.fragments.OcrappTabListener;
import at.ac.thornwishstalon.ocrapp.main.fragments.TextFragment;
import at.ac.thornwishstalon.ocrapp.ocr.TesseractHelper;
import at.ac.thornwishstalon.ocrapp.pic.preferences.PicPreferenceActivity;



public class Main extends Activity implements OnSharedPreferenceChangeListener{
	private final String TAG="at.ac.ocrap.main";
	private final String PREFS="at.ac.ocrap.prefs";
	private final int GET_IMAGE_FROM_GALLERY=	3246541;
	private final int GET_IMAGE_FROM_CAM=		2135723;
	private Uri last;
	private String mode;



	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(TAG, "creating Main");

		SharedPreferences settings=  getSharedPreferences(PREFS, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("ocrapp_image_uri", "");
		editor.putString("ocrapp_text", "");
		editor.commit();

		ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.setDisplayShowTitleEnabled(false);
		
		OcrappTabListener<ImageFragment> imageTabListener = new OcrappTabListener<ImageFragment>(this, ImageFragment.class);
		OcrappTabListener<TextFragment> textTabListener = new OcrappTabListener<TextFragment>(this, TextFragment.class);

		imageTabListener.setOtherFragment(textTabListener.getFragment());
		textTabListener.setOtherFragment(imageTabListener.getFragment());
		
		Tab tab = actionBar.newTab()
				.setText(R.string.main_ab_tab_image)
				.setIcon(R.drawable.content_picture)
				.setTabListener(imageTabListener);
		actionBar.addTab(tab);

		tab = actionBar.newTab()
				.setText(R.string.main_ab_tab_text)
				.setIcon(R.drawable.collections_view_as_list)
				.setTabListener(textTabListener);
		actionBar.addTab(tab);

		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		sharedPref.registerOnSharedPreferenceChangeListener(this);


		mode = sharedPref.getString("pic_prefs_mode_list", "0");
		
		if(mode.equals("0")){
			Log.d(TAG, "initialising tesseract. mode: DEFAULT");
			TesseractHelper.getInstance().initTesseract(TesseractHelper.DEFAULT);
		}
		else if(mode.equals("1")){
			Log.d(TAG, "initialising tesseract. mode: FAST");
			TesseractHelper.getInstance().initTesseract(TesseractHelper.FAST);
		}
		/*
		else if(mode.equals("2"))
			TesseractHelper.getInstance().initTesseract(TesseractHelper.MODERATE);
		else if(mode.equals("3"))
			TesseractHelper.getInstance().initTesseract(TesseractHelper.ACCURATE);
		*/
		else TesseractHelper.getInstance().initTesseract(TesseractHelper.DEFAULT);
		
		OCRAPPDialogHandler.getInstance().setActivity(this);
	}


	@Override
	protected void onDestroy() {
		Log.i(TAG, "destroying Main");
		super.onDestroy();
		TesseractHelper.getInstance().endTesseract();
	}

	@Override
	protected void onPause() {

		super.onPause();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case R.id.main_image_fragment_menu_settings:
			startActivity(new Intent(this, PicPreferenceActivity.class));
			break;
		case R.id.main_text_fragment_text_mene_copyToClipboard:
			break;
		case R.id.main_image_fragment_menu_source:
			Intent intent = new Intent();
			intent.setType("image/*");
			intent.setAction(Intent.ACTION_GET_CONTENT);
			startActivityForResult(Intent.createChooser(intent, getString(R.string.chooser_getContent)), GET_IMAGE_FROM_GALLERY);

			break;
		case R.id.main_image_fragment_menu_camera:
			Intent camIntent= new Intent();
			camIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
			last=OutputMedia.getOutputMediaFileUri();

			camIntent.putExtra(MediaStore.EXTRA_OUTPUT, last);
			startActivityForResult(camIntent, GET_IMAGE_FROM_CAM);

			break;
		case R.id.main_image_fragment_menu_doOCR:
			Log.d(TAG, "do ocr caught in main");
			break;
		}
		return super.onOptionsItemSelected(item);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.i(TAG, "got Activity result");
		if((resultCode== Activity.RESULT_OK) && (GET_IMAGE_FROM_GALLERY== requestCode )){

			Log.d(TAG,data.getData().toString());
			SharedPreferences settings=  getSharedPreferences(PREFS, 0);
			SharedPreferences.Editor editor = settings.edit();
			editor.putString("ocrapp_image_uri", data.getData().toString());
			editor.commit();
		}

		if((resultCode== Activity.RESULT_OK) && (GET_IMAGE_FROM_CAM== requestCode )){

			Log.d(TAG,"lalalal"+last.toString());
			SharedPreferences settings=  getSharedPreferences(PREFS, 0);
			SharedPreferences.Editor editor = settings.edit();
			editor.putString("ocrapp_image_uri", last.toString());
			editor.commit();
		}

	}



	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		
		if(key.equals("pic_prefs_mode_list")){
			mode = sharedPreferences.getString("pic_prefs_mode_list", "0");
			
			
			if(mode.equals("0")){
				Log.d(TAG, "initialising tesseract. mode: DEFAULT");
				TesseractHelper.getInstance().setMode(TesseractHelper.DEFAULT);
			}
			else if(mode.equals("1")){
				Log.d(TAG, "initialising tesseract. mode: FAST");
				TesseractHelper.getInstance().setMode(TesseractHelper.FAST);
			}
			else if(mode.equals("2"))
				TesseractHelper.getInstance().setMode(TesseractHelper.MODERATE);
			else if(mode.equals("3"))
				TesseractHelper.getInstance().setMode(TesseractHelper.ACCURATE);
		}

	}
	
	
}
